package topo.dialog;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Frame;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import topo.data.NodeLink;
import topo.service.TopoDataManager;
import topo.utils.DialogTools;

public class LinkViewDialog extends JDialog implements ActionListener {
    private NodeLink nodeLink;
    JPanel mainPanel = new JPanel();
    JPanel topPanel = new JPanel();
    JPanel buttonPanel = new JPanel();

    TitledBorder titledBorder = new TitledBorder("链接基本信息");

    JLabel linkNameLabel = new JLabel();
    JLabel fromNodeLabel = new JLabel();
    JLabel toNodeLabel = new JLabel();
    JLabel fromPhysicalPortLabel = new JLabel();
    JLabel fromInterfaceNameLabel = new JLabel();
    JLabel fromInterfaceIndexLabel = new JLabel();
    JLabel toPhysicalPortLabel = new JLabel();
    JLabel toInterfaceNameLabel = new JLabel();
    JLabel toInterfaceIndexLabel = new JLabel();
    JLabel linkBandWidthLabel = new JLabel();

    JTextField linkNameText = new JTextField();
    JTextField fromNodeText = new JTextField();
    JTextField toNodeText = new JTextField();
    JTextField fromPhysicalPortText = new JTextField();
    JTextField fromInterfaceNameText = new JTextField();
    JTextField fromInterfaceIndexText = new JTextField();
    JTextField toPhysicalPortText = new JTextField();
    JTextField toInterfaceNameText = new JTextField();
    JTextField toInterfaceIndexText = new JTextField();
    JTextField linkBandWidthText = new JTextField();

    JButton updateButton = new JButton("修改");
    JButton closeButton = new JButton("关闭");

    public LinkViewDialog(Frame frame, NodeLink link) {
        super(frame, "链接属性", true);
        try {
            this.nodeLink = link;

            initDialog();
        } catch (Exception ex) {
            ex.printStackTrace();
            DialogTools.showErroMsg(this, "查看链接出错!");
        }
    }

    private void initDialog() throws Exception {
        setDefaultCloseOperation(2);
        setTitle("链接属性");
        setSize(450, 290);
        setResizable(false);

        getContentPane().setLayout(new BorderLayout());
        this.topPanel.setBorder(this.titledBorder);
        this.topPanel.setLayout(null);

        this.linkNameLabel.setText("链接名称：");
        this.linkNameLabel.setBounds(10, 20, 100, 23);
        this.linkNameText.setText(this.nodeLink.getLinkDesc());
        this.linkNameText.setBounds(100, 20, 100, 23);

        this.fromNodeLabel.setText("链接起点：");
        this.fromNodeLabel.setBounds(10, 60, 100, 23);
        this.fromNodeText.setText(this.nodeLink.getFromNode());
        this.fromNodeText.setBounds(100, 60, 100, 23);
        this.toNodeLabel.setText("链接终点：");
        this.toNodeLabel.setBounds(new Rectangle(230, 60, 100, 23));
        this.toNodeText.setText(this.nodeLink.getToNode());
        this.toNodeText.setBounds(320, 60, 100, 23);

        this.fromPhysicalPortLabel.setText("起点物理端口号：");
        this.fromPhysicalPortLabel.setBounds(10, 100, 100, 23);
        if (this.nodeLink.getFromPhysicalPort() == -1) {
            this.fromPhysicalPortText.setText("未知端口号");
        } else {
            this.fromPhysicalPortText.setText(String.valueOf(this.nodeLink.getFromPhysicalPort()));
        }
        this.fromPhysicalPortText.setBounds(100, 100, 100, 23);
        this.toPhysicalPortLabel.setText("终点物理端口号:");
        this.toPhysicalPortLabel.setBounds(230, 100, 100, 23);
        if (this.nodeLink.getToPhysicalPort() == -1) {
            this.toPhysicalPortText.setText("未知端口号");
        } else {
            this.toPhysicalPortText.setText(String.valueOf(this.nodeLink.getToPhysicalPort()));
        }
        this.toPhysicalPortText.setBounds(320, 100, 100, 23);

        this.fromInterfaceNameLabel.setText("起点网络接口名:");
        this.fromInterfaceNameLabel.setBounds(10, 140, 100, 23);
        this.fromInterfaceNameText.setText(this.nodeLink.getFromNodeIfName());
        this.fromInterfaceNameText.setBounds(100, 140, 100, 23);
        this.toInterfaceNameLabel.setText("终点网络接口名:");
        this.toInterfaceNameLabel.setBounds(230, 140, 100, 23);
        this.toInterfaceNameText.setText(this.nodeLink.getToNodeIfName());
        this.toInterfaceNameText.setBounds(320, 140, 100, 23);

        this.fromInterfaceIndexLabel.setText("起点网络接口号:");
        this.fromInterfaceIndexLabel.setBounds(10, 180, 100, 23);
        if (this.nodeLink.getFromNodeIfIndex() == -1) {
            this.fromInterfaceIndexText.setText("未知接口号");
        } else {
            this.fromInterfaceIndexText.setText(String.valueOf(this.nodeLink.getFromNodeIfIndex()));
        }
        this.fromInterfaceIndexText.setBounds(100, 180, 100, 23);
        this.toInterfaceIndexLabel.setText("终点网络接口号:");
        this.toInterfaceIndexLabel.setBounds(230, 180, 100, 23);
        if (this.nodeLink.getToNodeIfIndex() == -1) {
            this.toInterfaceIndexText.setText("未知接口号");
        } else {
            this.toInterfaceIndexText.setText(String.valueOf(this.nodeLink.getToNodeIfIndex()));
        }
        this.toInterfaceIndexText.setBounds(320, 180, 100, 23);

        this.linkBandWidthLabel.setText("带宽(MByte/S):");
        this.linkBandWidthLabel.setBounds(230, 20, 100, 23);
        this.linkBandWidthText.setText(String.valueOf(this.nodeLink.getLinkBandwidth()));
        this.linkBandWidthText.setBounds(320, 20, 100, 23);

        this.fromNodeText.setEditable(false);
        this.toNodeText.setEditable(false);
        this.fromPhysicalPortText.setEditable(false);
        this.fromInterfaceNameText.setEditable(false);
        this.fromInterfaceIndexText.setEditable(false);
        this.toPhysicalPortText.setEditable(false);
        this.toInterfaceNameText.setEditable(false);
        this.toInterfaceIndexText.setEditable(false);
        this.linkBandWidthText.setEditable(false);

        this.linkNameText.setCaretPosition(0);
        this.fromInterfaceNameText.setCaretPosition(0);
        this.toInterfaceNameText.setCaretPosition(0);

        this.topPanel.add(this.linkNameLabel);
        this.topPanel.add(this.fromNodeLabel);
        this.topPanel.add(this.toNodeLabel);
        this.topPanel.add(this.fromPhysicalPortLabel);
        this.topPanel.add(this.fromInterfaceNameLabel);
        this.topPanel.add(this.fromInterfaceIndexLabel);
        this.topPanel.add(this.toPhysicalPortLabel);
        this.topPanel.add(this.toInterfaceNameLabel);
        this.topPanel.add(this.toInterfaceIndexLabel);
        this.topPanel.add(this.linkBandWidthLabel);
        this.topPanel.add(this.linkNameText);
        this.topPanel.add(this.fromNodeText);
        this.topPanel.add(this.toNodeText);
        this.topPanel.add(this.fromPhysicalPortText);
        this.topPanel.add(this.fromInterfaceNameText);
        this.topPanel.add(this.fromInterfaceIndexText);
        this.topPanel.add(this.toPhysicalPortText);
        this.topPanel.add(this.toInterfaceNameText);
        this.topPanel.add(this.toInterfaceIndexText);
        this.topPanel.add(this.linkBandWidthText);

        this.updateButton.addActionListener(this);
        this.closeButton.addActionListener(this);

        this.buttonPanel.add(this.updateButton);
        this.buttonPanel.add(this.closeButton);

        getContentPane().add(this.topPanel, "Center");
        getContentPane().add(this.buttonPanel, "South");
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == this.closeButton) {
            dispose();
        } else if (e.getSource() == this.updateButton) {
            this.nodeLink.setLinkDesc(this.linkNameText.getText().trim());

            TopoDataManager.getInstance().updateLink(this.nodeLink);
            dispose();
        }
    }
}